*===============================================================================
*
*					WORKER BELIEFS ABOUT OUTSIDE OPTIONS
*		(c)	Simon Jaeger, Christopher Roth, Nina Roussille, Benjamin Schoefer
*							  2023 December 5
*						   	   SOEP-IAB Data 
*
*===============================================================================


********************************************************************************
*								Figure IV	 								   *
********************************************************************************

cap log close                     
log using ${log}/FigureIV.log, replace 

* read in data: Full time, all movers
use "$data/FTp__finaldata.dta", clear	

global ytitle "Belief About Coworker Wage Change" 
	
*** final variable preparation
* winsorizing 'coworker wage changes' variable
cap drop actual_delta_ln_mover_change 
winsor mean_delta_ln_wage_mover_2015, gen(actual_delta_ln_mover_change) p(0.02) 

* winsorizing empirical bayes variable 
cap drop ebayes_delta_ln_mover_change
winsor ebayes_delta_ln_wage_mover_2015, gen(ebayes_delta_ln_mover_change) p(0.02)

* winsorizing split sample IV variable
cap drop actual_delta_ln_mover_change_r0 
winsor mean_delta_ln_wage_mover_r0_2015, gen(actual_delta_ln_mover_change_r0) p(0.02) 
cap drop actual_delta_ln_mover_change_r1 
winsor mean_delta_ln_wage_mover_r1_2015, gen(actual_delta_ln_mover_change_r1) p(0.02) 



*** creating the main exhibit plots with coworker wage changes as the benchmark
* obtaining coefficient and SE from regression of beliefs on raw mover wage changes
reg ln_mover_change_w actual_delta_ln_mover_change, cluster(pid)
local N_obs = e(N)
local beta = string(round(_b[actual_delta_ln_mover_change],0.001),"%4.3f")
local se = string(round(_se[actual_delta_ln_mover_change],0.001),"%4.3f")
local constant = _b[_cons]
local se_constant = _se[_cons]
				
* coefficient and SE from split-sample IV regression						
ivregress 2sls ln_mover_change_w (actual_delta_ln_mover_change_r1 = actual_delta_ln_mover_change_r0),  cluster(pid)
	
local N_obs_IV = e(N)
local beta_IV = string(round(_b[actual_delta_ln_mover_change_r1],0.001),"%4.3f")
local se_IV = string(round(_se[actual_delta_ln_mover_change_r1],0.001),"%4.3f")

					
* coefficient and SE from empirical bayes regression, if applicable
reg ln_mover_change_w ebayes_delta_ln_mover_change, cluster(pid)
local N_obs_EB = e(N)
local beta_EB = string(round(_b[ebayes_delta_ln_mover_change],0.001),"%4.3f")
local se_EB = string(round(_se[ebayes_delta_ln_mover_change],0.001),"%4.3f")
local constant_EB = string(round(_b[_cons],0.001),"%4.3f")
local extraEB "Empirical Bayes Slope: `beta_EB' (SE `se_EB')"
			
* erase help files
cap erase "$data/binscdata.do"
cap erase "$data/binscdata.csv"
cap erase "$data/binscdata_EB.do"
cap erase "$data/binscdata_EB.csv"
					
* empirical bayes line							
binscatter ln_mover_change_w ebayes_delta_ln_mover_change ebayes_delta_ln_mover_change, savedata("$data/binscdata_EB")
preserve
	clear
	do "$data/binscdata_EB"
	gen mergevar = _n
	rename ln_mover_change_w ln_mover_change_w_eb
	save "$data/ebplots_temp.dta", replace
restore
					
* preparing the raw data line
cap noisily binscatter ln_mover_change_w actual_delta_ln_mover_change actual_delta_ln_mover_change, savedata("$data/binscdata")
					
* constructing the plot
clear 							
do "$data/binscdata" // loading raw data line
gen mergevar = _n // merging on EB line
merge 1:1 mergevar using "$data/ebplots_temp.dta", keep(master match) nogen

* setting axis ranges
local max = 1
local min = -0.6

local slopemin = `min' 
local slopemax = `max'

foreach rangevar in ln_mover_change_w actual_delta_ln_mover_change {
	quietly su `rangevar'
	if r(min)<`slopemin' local slopemin = r(min)
	if r(max)>`slopemax' local slopemax = r(max)
}

twoway connect ln_mover_change_w actual_delta_ln_mover_change if ln_mover_change_w==69, lc(navy) mc(navy) msymbol(circle) ///
	|| connect ln_mover_change_w actual_delta_ln_mover_change if ln_mover_change_w==69, lc(orange) mc(orange) lpattern(dash_dot) msymbol(triangle_hollow) ///
	|| function y = x, range(`min' `max') color(black%50) ///
	|| function y = (`beta_IV'*x)+`constant', range(`slopemin' `slopemax') color(maroon) lpattern(longdash) ///
	|| function y = (`beta_EB'*x)+`constant', range(`slopemin' `slopemax') color(navy) ///
	|| scatter ln_mover_change_w_eb ebayes_delta_ln_mover_change, mcolor(navy) msymbol(circle) ///
	|| scatter ln_mover_change_w actual_delta_ln_mover_change, mcolor(orange) msymbol(triangle_hollow)  ///
	|| function y = (`beta'*x)+`constant', range(`slopemin' `slopemax') color(orange) lpattern(dash_dot) ///
	, legend(order(1 "Empirical Bayes Slope: `beta_EB' (SE `se_EB')" 4 "Split-Sample IV Slope:  `beta_IV' (SE `se_IV')" 2 "Unadjusted Slope:         `beta' (SE `se')") cols(1) ring(0) size(small) position(11) region(lwidth(none))) legend(size(medsmall)) ///
	xscale(range(`min' `max')) yscale(range(`min' `max')) ylabel(#10,grid) xlabel(#10,grid) ///
	xtitle("Objective Benchmark: Mover Wage Changes") ytitle("${ytitle}") 
	addplot: scatteri 0.425 0.35 "Accurate Beliefs: Slope = 1", msymbol(i) mlabpos(0) mlabangle(35) mlabcolor(black) legend(order(1 "Empirical Bayes Slope: `beta_EB' (SE `se_EB')" 4 "Split-Sample IV Slope:  `beta_IV' (SE `se_IV')" 2 "Unadjusted Slope:         `beta' (SE `se')") cols(1) ring(0) size(small) position(11) region(lwidth(none))) legend(size(medsmall))
	graph export ${log}/FigureIV.pdf, replace

	
log close 
clear
